/**
* ...
* @author Default
* @version 0.1
*/

package org.anivision.eval {
	
	import com.hurlant.eval.*;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.* ;
	import flash.utils.ByteArray;

	public class Eval extends EventDispatcher{
		
		private var _evaluator:Evaluator;
		private var _l:LoaderInfo;
		public var history:String = '';
		
		private static var _e:Eval;
		public static function get singleton():Eval {
			return _e;
		}
		
		public function Eval():void {
			_evaluator = new Evaluator();
			if ( Eval._e == null )
				Eval._e = this;
		}
		
		public function eval(code:String):void
		{
			add_history(code);
			compile(parse(code));
		}
		
		public function parse(code:String):ByteArray
		{
			_evaluator = new Evaluator();
//			try {
				return _evaluator.eval(code);
/*			}
			catch (e:Error) {
				add_history(e.message);
			}
			finally {
				return new ByteArray();
			}*/
		}
		
		private function add_history(str:*):void {
			var s:String = String(str);
			s.replace("\r\n", "");
			s.replace("\n\r", "");
			history += s + "\n";
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function compile(bytes:ByteArray):void
		{
			if ( bytes.length > 0)
				ByteLoader.loadBytes(bytes, true);
		}
	}
}
